/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

/**
 *
 * @author ozgencetinkaya
 */

import DAO.EventDAO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import javax.servlet.ServletException;
import java.io.*;
import java.util.*;
import DAO.UserDAO;
import connection.ApiProxyClient;
import model.EventBean;
import model.UserBean;
import model.UstreamBean;

@Controller
@RequestMapping("/willingwalkerevents.htm")
public class WillingWalkerEventsController {
    
    @RequestMapping(method = RequestMethod.GET)
    public String showEvents(Map model,HttpServletRequest request,HttpServletResponse response) throws IOException {
    
        
        HttpSession session = request.getSession(true);
        UserBean userBean = (UserBean)(session.getAttribute("currentSessionUser"));
        
        
        EventDAO eventDAO = new EventDAO();
        List<EventBean> eventBeans = eventDAO.getWillingWalkerEvents(userBean);
        
        for(int i=0;i<eventBeans.size();i++){
            String ustreamUserName = eventDAO.getUstreamUserNameOfCreatorOfEvent(eventBeans.get(i).getEventID());
            ApiProxyClient apc = new ApiProxyClient(ustreamUserName);
            UstreamBean ustreamBean = apc.getInfo();
            
            if(ustreamBean.getResults().getStatus().equals("offline")){
                eventBeans.get(i).setStatus(0);
            }
            else{
                eventBeans.get(i).setStatus(1);
            }
                
        }
        
        model.put("numberOfEvents",eventBeans.size());
        model.put("events",eventBeans);
        
        return "willingwalkerevents";
    }
}
